/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.client.inGame;
import core.messages.client.inGame.Msg_Response;
import core.messages.client.inGame.Msg_Response_No;
import core.messages.client.inGame.Msg_Response_Yes;
import core.threads.DataBase;
import core.threads.client.Client_Public_Data;
import core.threads.client.Client_Public_MsgBox;
import core.threads.client.Client_Public_Thread;
import cstAndEnum.SKILL_TYPE;
import java.util.ArrayList;
import java.util.List;
import models.Card.Card;
import models.Card.CardSpell;
import models.CastInfo;
import models.Player;
import models.Skill.Skill;
import myTest.MYTest;

/**
 *
 * @author Invince
 */
public class T_Client_Responser  extends Thread{
/*----------Attributs-------------------*/
//    protected boolean stop;
//    protected boolean suspend;

    protected CastInfo previousCastInfo;
/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        String name;
        synchronized(Client_Public_Data.semaMyInfo){
            name=Client_Public_Data.myInfo.getName();
        }
        boolean isMyTurn;
        synchronized(Client_Public_Data.semaIsMyTurn){
            isMyTurn=Client_Public_Data.isMyTurn;
        }
        if(!isMyTurn){//有可能不需要
            Client_Public_Thread.tClientActiveSkill.resumeT();
        }
        /**-----Main----**/
        Long tBegin=System.currentTimeMillis();
        Long tEnd=System.currentTimeMillis();
        while((tEnd-tBegin)<15*1000){//15*1000 ms
            if(!Client_Public_MsgBox.msgBox_Client_Response.isEmpty()){
                Msg_Response msgR=Client_Public_MsgBox.msgBox_Client_Response.poll();
                if(msgR instanceof Msg_Response_No){//放弃回应
                    MYTest.println("You don't choose to respond him");
                    Msg_Response_No msgRN=(Msg_Response_No) msgR;
                    Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgRN);
                    break;
                }else if(msgR instanceof Msg_Response_Yes){//有回应
                    Msg_Response_Yes msgRY=(Msg_Response_Yes) msgR;
                    CastInfo castInfo=msgRY.getCastInfo();
                    if(!isResponsable(castInfo)){
                        MYTest.println("You can't respond with this card or Skill! Rechoose one.");
                    }else{
                        Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgRY);
                        MYTest.println("You respond him");
                        break;
                    }
                }
            }
            tEnd=System.currentTimeMillis();
        }
        if((tEnd-tBegin)>=15*1000){//说明是超时出循环的
            Msg_Response_No msgRN=new Msg_Response_No(name, "Server");
            Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgRN);
        }
        /**----End-----**/
        if(!isMyTurn){//有可能不需要
            Client_Public_Thread.tClientActiveSkill.suspendT();
        }
    }

//    public void stopT(){
//        synchronized(this){
//            this.stop=true;
//        }
//    }
//
//    public void suspendT(){
//        synchronized(this){
//            this.suspend=true;
//        }
//    }
//
//    public void resumeT(){
//        synchronized(this){
//            this.suspend=false;
//        }
//    }
/*----------Private functions-----------*/
    private boolean isResponsable(CastInfo castInfo){//？？？？？？这个还未完成
        if(this.previousCastInfo.getCard() instanceof CardSpell){
            if(castInfo.getSkill().getName().equals("无懈可击")){
                return true;
            }
        }else
        if(this.previousCastInfo.getSkill().getName().equals("杀")){
            if(castInfo.getSkill().getName().equals("闪")){
                return true;
            }
        }else
        if(this.previousCastInfo.getSkill().getName().equals("南蛮入侵")){//注意无懈可击一开始判断了
            if(castInfo.getSkill().getName().equals("杀")){
                return true;
            }
        }else
        if(this.previousCastInfo.getSkill().getName().equals("万箭齐发")){
            if(castInfo.getSkill().getName().equals("闪")){
                return true;
            }
        }else
        if(this.previousCastInfo.getSkill().getName().equals("决斗")){//无懈可击挡决斗在最开始就判断了
            if(castInfo.getSkill().getName().equals("杀")){
                return true;
            }
        }else
        if(this.previousCastInfo.getSkill().getName().equals("继续决斗")){//注意继续决斗是指进入决斗状态了，此时不能再出无懈可击了
            if(castInfo.getSkill().getName().equals("杀")){
                return true;
            }
        }
        return false;
    }
/*----------Constructor-----------------*/
    public T_Client_Responser(CastInfo previousCastInfo) {
        synchronized(this){
            this.previousCastInfo=previousCastInfo;
//            this.stop=false;
//            this.suspend=false;
        }
    }

    public T_Client_Responser(int idSkill, int idSource, int[] idTargetPlayers, int idCard,int[] idTargetCards, SKILL_TYPE skillType) {
        synchronized(this){
            Skill skill=DataBase.findSkillByID(idSkill);
            Card card=DataBase.findCardByID(idCard);
            Player source;
            List<Player> targets=new ArrayList<Player>();
            synchronized(Client_Public_Data.semaGameInfoClient){
                source=Client_Public_Data.gameInfoClient.getListPlayer().findPlayerById(idSource);
                for(int i:idTargetPlayers){
                    targets.add(Client_Public_Data.gameInfoClient.getListPlayer().findPlayerById(idTargetPlayers[i]));
                }
            }
            List<Card> targetCards=new ArrayList<Card>();
            for(int i:idTargetCards){
                targetCards.add(DataBase.findCardByID(i));
            }
            this.previousCastInfo=new CastInfo(source, targets, skill, card,targetCards,skillType);
//            this.stop=false;
//            this.suspend=false;
        }
    }
/*----------Gets and sets---------------*/
}
